<template>
    <div style="width: 100%">
        <div id="deawer">
            <v-tableTitle :btnConfig="btnConfig" @cancel="cancel" @save="save" @allocation="allocation"
                          @grant="grant" @revocation="revocation" @order="order" @deliver="deliver"></v-tableTitle>
            <div class="detailBox">
                <v-detail ref="detail"></v-detail>
            </div>
            <div class="detailListBox">
                <v-detailList ref="detailList"></v-detailList>
            </div>
        </div>
    </div>

</template>

<script>
    import baseMixin from "@/mixins/BaseMixin";
    import skipMixin from "@/mixins/SkipMixin";

    import tableTitle from "commons/TableTitle";
    import detail from "../details/Detail"
    import detailList from "../detailsList/DetailList"
    import table from "commons/Table";
    import config from "../Config";

    export default {
        name: "",
        mixins: [baseMixin, skipMixin, config],
        data() {
            return {
                newFlag: false,
                routeData: {}
            };
        },
        computed: {
            btnConfig() {
                if (this.newFlag) {
                    return [
                        {label: this.$t('common.save'), certigier: this.saveStatus||!this.btnPermNameList.includes('WM020409'), invalid: "save"},
                        {label: this.$t('common.return'), certigier: !this.btnPermNameList.includes('WM020416'), invalid: "cancel"},
                    ]
                } else {
                    return [
                        {label: this.$t('common.save'), certigier: this.saveStatus||!this.btnPermNameList.includes('WM020409'), invalid: "save"},
                        {
                            type: "more", label: this.$t('common.moreMenu'),certigier: !this.btnPermNameList.includes('WM020410'),  invalid: "down", btnConfig: [
                                {label: this.$t('out.allocation'), certigier: this.allocationStatus||!this.btnPermNameList.includes('WM020411'), invalid: "allocation"},
                                {label: this.$t('out.grant'), certigier: this.allocationStatus||!this.btnPermNameList.includes('WM020412'), invalid: "grant"},
                                {label: this.$t('out.revocation'), certigier: this.allocationStatus||!this.btnPermNameList.includes('WM020413'), invalid: "revocation"},
                                {label: this.$t('out.allPick'), certigier: this.allocationStatus||!this.btnPermNameList.includes('WM020414'), invalid: "order"},
                                {label: this.$t('out.allShip'), certigier: this.allocationStatus||!this.btnPermNameList.includes('WM020415'), invalid: "deliver"},
                            ]
                        },
                        {label: this.$t('common.return'),certigier: !this.btnPermNameList.includes('WM020416'), invalid: "cancel"},
                    ]
                }

            }
        },
        created() {
        },
        methods: {
            async deliver() {
                try {
                    let res = await this.$api.get(`/ordersDeliverAll/${this.routeData.orderkey}`);
                    
                        this.$message.success(res.returnMgr);
                    
                } catch (e) {
                    this.$message.error(e);
                }
            },
            async order() {
                try {
                    let res = await this.$api.get(`/ordersPickingAll/${this.routeData.orderkey}`);
                    
                        this.$message.success(res.returnMgr);
                    
                } catch (e) {
                    this.$message.error(e);
                }
            },
            async revocation() {
                try {
                    let res = await this.$api.get(`/ordersRevokeAllocate/${this.routeData.orderkey}`);
                    
                        this.$message.success(res.returnMgr);
                    
                } catch (e) {
                    this.$message.error(e);
                }
            },
            async allocation() {
                try {
                    let res = await this.$api.post(`/allocateByOrderkey/${this.routeData.orderkey}/${this.$store.state.wms}`);
                    
                        this.$message.success(res.returnMgr);
                    
                } catch (e) {
                    this.$message.error(e);
                }
            },
            async grant() {
                try {
                    let res = await this.$api.get(`/ordersReleaseTask/${this.routeData.orderkey}`);
                    
                        this.$message.success(res.returnMgr);
                    
                } catch (e) {
                    this.$message.error(e);
                }
            },
            //保存按钮
            async save() {
                let detail = this.toEmpty(this.$refs.detail.detailData);
                let detailList = this.toEmpty(this.$refs.detailList.detailData);
                let data = {};
                if (this.newFlag) {
                    data = {
                        ...detail,
                        orderdetail: [{...detailList}]
                    }
                } else {
                    if (this.$store.state.temporary) {
                        data = {
                            ...detail,
                            orderdetail: [{...detailList}]
                        }
                    } else {
                        data = {
                            ...detail
                        }
                    }
                }
                try {
                    let res = await this.$api.post('/excuteOrders', data);
                    
                        this.$message.success(res.returnMgr);
                        this.$store.commit("setDetailsNew", false);
                        this.cancel();
                    
                } catch (e) {
                    this.$message.error(e)
                }
            },
            //弹框取消
            cancel() {
              let routerName = JSON.parse(sessionStorage.getItem("routerName"));
              let num = -1;
              if (routerName) {
                num = routerName.indexOf("orderRtx");
              }
              if (num === -1) {
                this.$router.push({ name: "orderRtx" });
              } else {
                num = num - 0;
                routerName.splice(num, 1);
                sessionStorage.setItem("routerName", JSON.stringify(routerName));
                this.$router.push({ name: "home1" });
              }
                /*this.$router.go(-1);
                this.$store.commit("setDetailsNew", false);*/
            },
        },
        mounted() {
            if (Object.keys(this.$store.state.skip).length !== 0) {
                let data = this.$store.state.skip;
                this.routeData = data.routeData;
                this.newFlag = data.newFlag;
            } else if (this.$store.state.newFlag) {
                this.newFlag = true;
            } else {
                this.routeData = this.$store.state.routeData
            }
        },
        watch: {},
        components: {
            "v-tableTitle": tableTitle,
            "v-detail": detail,
            "v-detailList": detailList,
            "v-table": table
        }
    }
</script>

<style scoped>

</style>
